import sys

import pygame
from bullet import Bullet


def check_keydown_events(event, ai_settings, screen, ship, bullets):
    """响应按下按键"""
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        bullets.add(Bullet(ai_settings, screen, ship))


def check_keyup_events(event, ship):
    """响应松开按键"""

    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False


def check_events(ai_settings, screen, ship, aliens, bullets, button):
    """响应键盘和鼠标"""
    if ai_settings.is_begin:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            elif event.type == pygame.KEYDOWN:
                aliens_random(aliens)
                check_keydown_events(event, ai_settings, screen, ship, bullets)

            elif event.type == pygame.KEYUP:
                aliens_random(aliens)
                check_keyup_events(event, ship)
    else:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                  sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if button.rect.collidepoint(pygame.mouse.get_pos()):
                    ai_settings.is_begin = True


def aliens_random(aliens):
    """随机改变外星人的运行区间"""
    for alien in aliens.sprites():
        alien.random_offset()


def update_model(screen, ship, aliens, bullets):
    """更新各个模型的相关数据"""
    ship.update()
    # 外星人边界检查
    for alien in aliens.sprites():
        alien.check()
    update_aliens(screen, ship, aliens)
    update_bullets(aliens, bullets)


def update_screen(ai_settings, screen, ship, aliens, bullets, button):
    """更新屏幕以及屏幕上的模型"""
    screen.fill(ai_settings.bg_color)

    ship.blitme()
    aliens.draw(screen)

    for bullet in bullets.sprites():
        bullet.blitme()
    if not ai_settings.is_begin:
        button.draw_button()
    pygame.display.flip()


def update_bullets(aliens, bullets):
    """更新子弹的位置，并删除旧子弹"""
    # 更新子弹位置
    bullets.update()

    # 去除已经消失的子弹
    for bullet in bullets.sprites():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

    check_bullet_alien_collisions(aliens, bullets)


def check_bullet_alien_collisions(aliens, bullets):
    """应对子弹与外星人的碰撞"""
    # 清除所有相撞的子弹和外星人
    pygame.sprite.groupcollide(bullets, aliens, True, True)

    if len(aliens) == 0:
        # 如果整个舰队被摧毁，则开始新的关卡
        bullets.empty()


def ship_hit():
    """应对被外星人袭击的船只"""
    sys.exit()


def check_aliens_bottom(screen, aliens):
    """检查是否有外星人到达屏幕底部"""
    screen_rect = screen.get_rect()
    for alien in aliens.sprites():
        if alien.rect.bottom >= screen_rect.bottom:
            ship_hit()
            break


def update_aliens(screen, ship, aliens):
    """
    检查舰队是否处于边缘，然后更新舰队中所有外星人的位置
    """
    aliens.update()
    # 检测外星人碰撞
    if pygame.sprite.spritecollideany(ship, aliens):
        ship_hit()

    # 检测外星人撞到屏幕底部
    check_aliens_bottom(screen, aliens)
